﻿using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;
using System.Windows.Controls;
using System.Windows.Input;
using SilverAmp.Common;
using SilverAmp.Common.DecoupledEvents;
using SilverAmp.Shared;
using System.Linq;

namespace SilverAmp
{

    public class PluginsViewModel : INotifyPropertyChanged, IPartImportsSatisfiedNotification
    {

        [Import]
        public ExtensionsModel Model { get; set; }
        
        private ISilverAmpExtension _selectedExtension;
        public ISilverAmpExtension SelectedExtension
        {
            get { return _selectedExtension; }
            set
            {
                if (_selectedExtension != value)
                {
                    _selectedExtension = value;
                    InvokePropertyChanged("SelectedExtension");
                }
            }
        }

        public ICommand ChangeSelectedExtension { get; set; }
        public ICommand ShowMenu { get; set; }


        public PluginsViewModel()
        {                   

            InitializeMEF();
            Model.Extensions.CollectionChanged += delegate
                                                      {
                                                          InvokePropertyChanged("Model");
                                                      };

            ChangeSelectedExtension = new DelegateCommand<ISilverAmpExtension>(OnChangeSelectionExtension);         
            DecoupledEventAggregator.GetInstance().GetEvent<ShowMenuEvent>().Subscribe(OnShowMenu);
            DecoupledEventAggregator.GetInstance().GetEvent<EditSongEvent>().Subscribe(OnEditSong);
        }

        


        [Import]
        public IDeploymentCatalogService service;
        private void InitializeMEF()
        {
           DeploymentCatalogService.Initialize();            
             CompositionInitializer.SatisfyImports(this);
           service.AddXap("SilverAmp.LastFmExtensions.xap");
          
        }

        private void OnEditSong(ITrackInfo trackInfo)
        {
            var extension = Model.Extensions.Where(x => x.Name == "EditSong").FirstOrDefault();
            OnChangeSelectionExtension(extension); 
        }

        private void OnShowMenu(string obj)
        {
            var extension = Model.Extensions.Where(x => x.Name == "Menu").FirstOrDefault();
            OnChangeSelectionExtension(extension);            
        }

        private void OnChangeSelectionExtension(ISilverAmpExtension obj)
        {
           
            if (obj != null)
            { 
                SelectedExtension = obj;
                SelectedExtension.Initialize();
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        public void InvokePropertyChanged(string e)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (PropertyChanged != null)
                PropertyChanged(this,
                    new PropertyChangedEventArgs(
                        e));
        }

        public void OnImportsSatisfied()
        {
            InvokePropertyChanged(string.Empty);
        }
    }

    public class SilverAmpExtension : ISilverAmpExtension
    {
        public string Name { get; set; }
        public string DisplayIcon { get; set; }
        public UserControl ExtensionUi { get; set; }

        public void Initialize()
        {
            //return ExtensionUi;
        }

        public bool IsVisibleInMenu { get; set; }
    }
}
